package api

import (
	"encoding/json"
	"git.supremind.info/product/box/vehicle/internal/dao"
	"git.supremind.info/product/box/vehicle/internal/dao/req"
	"git.supremind.info/product/box/vehicle/internal/dao/res"
	"git.supremind.info/product/box/vehicle/internal/dto"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetHisDataList(c *gin.Context) {
	var request req.ModelHisDataListReq
	if err := c.Bind(&request); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
		return
	}

	events, total, err := dao.GetModelHisDataList(request)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
	}

	vehicleModelDataResponses := make([]dto.VehicleModelDataResponse, 0)
	for _, event := range events {
		var veh = dto.VehicleModelDataResponse{
			ID:                 event.ID,
			Gantry:             event.Gantry,
			EventId:            event.EventId,
			CompanyId:          event.CompanyId,
			OrganizationId:     event.OrganizationId,
			OrganizationPath:   event.OrganizationPath,
			Source:             event.Source,
			VehicleType:        event.VehicleType,
			BoxName:            event.BoxName,
			SerialNumber:       event.SerialNumber,
			AxlesNumber:        event.AxlesNumber,
			WheelsNumber:       event.WheelsNumber,
			HazardousChemicals: event.HazardousChemicals,
			Lane:               event.Lane,
			EventTime:          event.EventTime.String(),
			PushStatus:         event.PushStatus,
			IsDel:              event.IsDel,
			CreateBy:           event.CreateBy,
			CreateTime:         event.CreateTime.String(),
			UpdateBy:           event.UpdateBy,
			UpdateTime:         event.UpdateTime.String(),
		}
		m := make(map[string]interface{})
		err := json.Unmarshal([]byte(event.Data), &m)
		if err != nil {
			continue
		}
		veh.Data = m
		vehicleModelDataResponses = append(vehicleModelDataResponses, veh)
	}

	response := &res.ModelHisDataListResponse{
		PageNo:        request.PageNo,
		PageSize:      request.PageSize,
		Total:         total,
		Pages:         (total + request.PageSize - 1) / request.PageSize,
		Code:          0,
		ModelDataList: vehicleModelDataResponses,
	}

	c.JSON(http.StatusOK, response)
}

func DelHisData(c *gin.Context) {
	var req struct {
		Ids []int `json:"ids"`
	}

	if c.BindJSON(&req) != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "用户数据解析失败"})
		return
	}

	dao.DeleteByIds(req.Ids)
	c.JSON(http.StatusOK, gin.H{"code": 0, "message": "请求成功"})
}

func QueryCount(c *gin.Context) {
	var count int64
	dao.DB.Model(&dao.VehicleModelData{}).Count(&count)
	c.JSON(http.StatusOK, gin.H{"code": 0, "data": count, "message": "请求成功"})
}

func GetTypeConstList(c *gin.Context) {
	vehicleTypes := make([]map[string]string, 15)
	vehicleTypes[0] = assembleCodeNameMap("carriage_1", "一型客车")
	vehicleTypes[1] = assembleCodeNameMap("carriage_2", "二型客车")
	vehicleTypes[2] = assembleCodeNameMap("carriage_3or4", "三或四型客车")

	vehicleTypes[3] = assembleCodeNameMap("truck_1", "一型货车")
	vehicleTypes[4] = assembleCodeNameMap("truck_2", "二型货车")
	vehicleTypes[5] = assembleCodeNameMap("truck_3", "三型货车")
	vehicleTypes[6] = assembleCodeNameMap("truck_4", "四型货车")
	vehicleTypes[7] = assembleCodeNameMap("truck_5", "五型货车")
	vehicleTypes[8] = assembleCodeNameMap("truck_6", "六型货车")

	vehicleTypes[9] = assembleCodeNameMap("special_operation_1", "一型专项作业车")
	vehicleTypes[10] = assembleCodeNameMap("special_operation_2", "二型专项作业车")
	vehicleTypes[11] = assembleCodeNameMap("special_operation_3", "三型专项作业车")
	vehicleTypes[12] = assembleCodeNameMap("special_operation_4", "四型专项作业车")
	vehicleTypes[13] = assembleCodeNameMap("special_operation_5", "五型专项作业车")
	vehicleTypes[14] = assembleCodeNameMap("special_operation_6", "六型专项作业车")

	hazardousChemicals := make([]map[string]string, 9)
	hazardousChemicals[0] = assembleCodeNameMap("flammable", "易燃物品")
	hazardousChemicals[1] = assembleCodeNameMap("explosive", "易爆物品")
	hazardousChemicals[2] = assembleCodeNameMap("corrosive", "腐蚀性物品")
	hazardousChemicals[3] = assembleCodeNameMap("toxicity", "毒性物品")
	hazardousChemicals[4] = assembleCodeNameMap("inert_gas", "惰性气体")
	hazardousChemicals[5] = assembleCodeNameMap("oxidizing", "氧化性物品")
	hazardousChemicals[6] = assembleCodeNameMap("miscellaneous", "杂项危险品")
	hazardousChemicals[7] = assembleCodeNameMap("infectious", "感染性物品")
	hazardousChemicals[8] = assembleCodeNameMap("radioactive", "放射性物品")

	plateColors := make([]map[string]string, 6)
	plateColors[0] = assembleCodeNameMap("blue", "蓝色")
	plateColors[1] = assembleCodeNameMap("yellow", "黄色")
	plateColors[2] = assembleCodeNameMap("white", "白色")
	plateColors[3] = assembleCodeNameMap("green", "绿色")
	plateColors[4] = assembleCodeNameMap("black", "黑色")
	plateColors[5] = assembleCodeNameMap("none", "-")

	axlesNumbers := make([]string, 5)
	axlesNumbers[0] = "2"
	axlesNumbers[1] = "3"
	axlesNumbers[2] = "4"
	axlesNumbers[3] = "5"
	axlesNumbers[4] = ">=6"

	vehicleDirection := make([]map[string]string, 2)
	vehicleDirection[0] = assembleCodeNameMap("left", "从左向右")
	vehicleDirection[1] = assembleCodeNameMap("right", "从右向左")

	var res struct {
		VehicleTypes       []map[string]string `json:"vehicleTypes"`
		PlateColors        []map[string]string `json:"plateColors"`
		HazardousChemicals []map[string]string `json:"hazardousChemicals"`
		AxlesNumbers       []string            `json:"axlesNumbers"`
		VehicleDirection   []map[string]string `json:"vehicleDirection"`
	}

	res.VehicleTypes = vehicleTypes
	res.HazardousChemicals = hazardousChemicals
	res.PlateColors = plateColors
	res.AxlesNumbers = axlesNumbers
	res.VehicleDirection = vehicleDirection

	c.JSON(http.StatusOK, dto.Result{
		Code:    0,
		Message: "请求成功",
		Data:    res,
	})
}

func assembleCodeNameMap(code, name string) map[string]string {
	var o = make(map[string]string)
	o["label"] = name
	o["value"] = code
	return o
}
